//
//  MovieController.m
//  PlayMovie
//
//  Created by 畑圭輔 on 09/10/13.
//  Copyright 2009 Keisuke Hata. All rights reserved.
//

#import "MovieController.h"
#import "SimpleMoviePlayer_Delegate.h"

@implementation MovieController

@synthesize movieURL = movieURL_;

#pragma mark -
#pragma mark notification

// ムービーのロード完了直前の通知
- (void) moviePreloadDidFinish:(NSNotification*)notification
{
	[moviePlayer_ play];
	NSLog(@"ムービー再生");
}

// ムービー再生完了の通知
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
	/* コードを追加して下さい */
	NSLog(@"ムービー再生終了");
}

// スケールモードを変更した際の通知
- (void) movieScalingModeDidChange:(NSNotification*)notification
{
	/* コードを追加して下さい */
	NSLog(@"スケールモードの変更");
}

#pragma mark -
#pragma mark initialize

-(void)initMoviePlayer
{
	// ムービープレイヤーを生成する
	moviePlayer_ = [[MPMoviePlayerController alloc] initWithContentURL:self.movieURL];

	// スケーリングモードを設定
    moviePlayer_.scalingMode      = MPMovieScalingModeNone;
	
	// ムービーコントローラーの表示設定
    moviePlayer_.movieControlMode =	MPMovieControlModeDefault;
	
	// ムービーを読み込む時のフェード色
    moviePlayer_.backgroundColor = [UIColor blackColor];
	
	// 再生初期位置を設定( iPhone OS3.0から利用可能 )
	if( [moviePlayer_ respondsToSelector:@selector(initialPlaybackTime)] )
	{
		moviePlayer_.initialPlaybackTime = 0.0f;
		NSLog(@"iPhone OS3.0以上の為利用可能");
	}else {
		NSLog(@"iPhone OS2.xの為利用不可");
	}

	
	// ムービー制御関連の通知を登録
	[[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(moviePreloadDidFinish:) 
												 name:MPMoviePlayerContentPreloadDidFinishNotification 
											   object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(moviePlayBackDidFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification
											   object:moviePlayer_];
	
    [[NSNotificationCenter defaultCenter] addObserver:self 
											 selector:@selector(movieScalingModeDidChange:) 
												 name:MPMoviePlayerScalingModeDidChangeNotification 
											   object:moviePlayer_];
	
}


#pragma mark View Controller Routines

// 動画のURLを返す
-(NSURL *) movieURL
{
    if (movieURL_ == nil)
    {
        NSBundle *bundle = [NSBundle mainBundle];
        if (bundle) 
        {
            NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
            if (moviePath)
            {
                movieURL_ = [NSURL fileURLWithPath:moviePath];
				[movieURL_ retain];
            }
        }
    }
    return movieURL_;
}

- (void)dealloc {
    
	// URLの開放
    [movieURL_ release];
    
    // ムービー制御関連の通知を削除する
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerContentPreloadDidFinishNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayer_];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerScalingModeDidChangeNotification
                                                  object:moviePlayer_];
	
	// プレイヤーの開放
    [moviePlayer_ release];
    
    [super dealloc];
}



@end
